package com.gengealogy.net.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gengealogy.net.po.Baby;
import com.gengealogy.net.service.BabyService;

@Controller
public class BabyController {
	@Autowired
	private BabyService babyService;

	public BabyService getBabyService() {
		return babyService;
	}

	public void setBabyService(BabyService babyService) {
		this.babyService = babyService;
	}

	/**
	 * 向传家宝页面跳转
	 */
	@RequestMapping(value = "/build/getBaby.action")
	public String getBaby(Model model, HttpSession session) {

		int baby_tree_id = (int) session.getAttribute("TREE_ID");
		List<Baby> baby = babyService.findBaby(baby_tree_id);
		if (baby == null) {
			model.addAttribute("msg", "空空如也，该家族还没有传家宝，记得上传哦！");
			return "build/build_baby";
		} else {
			model.addAttribute("baby", baby);
			int user_ident = (int) session.getAttribute("USER_IDENT");
			if (user_ident == 0)
				// 跳转到普通用户主页面
				return "user/user_baby";
			else
				return "build/build_baby";
		}

	}

	/**
	 * 向创建族谱页面跳转
	 */
	@RequestMapping(value = "/addBaby.action", method = RequestMethod.GET)
	public String toAddBaby() {
		return "build/build_addBaby";
	}

	@RequestMapping(value = "/addBaby.action", method = RequestMethod.POST)
	public String addBaby(Baby baby, Model model) {
		babyService.addBaby(baby);
		return "build/build_baby";
	}

}
